using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Admin_ProductType : System.Web.UI.UserControl
{
    processProductType pProductType;
    protected void Page_Load(object sender, EventArgs e)
    {
        pProductType = new processProductType();

        if (!Page.IsPostBack)
        {
            LoadDataGvwProductType();
        }  
    }
    private void LoadDataGvwProductType()
    {
        gvwProductType.DataSource = pProductType.getProductTypes();
        gvwProductType.DataBind();
    }


    protected void gvwProductType_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvwProductType.EditIndex = e.NewEditIndex;
        LoadDataGvwProductType();
    }
    protected void gvwProductType_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvwProductType.EditIndex = -1;
        LoadDataGvwProductType();
    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        string ProductTypeID = pProductType.getProductTypeID();
        if (pProductType.insertProductType(ProductTypeID, "") > 0)
        {
            gvwProductType.EditIndex = gvwProductType.Rows.Count;
            LoadDataGvwProductType();
        }
    }
    protected void gvwProductType_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            string ProductTypeID = gvwProductType.DataKeys[e.RowIndex].Value.ToString();
            if (pProductType.deleteProductType(ProductTypeID) > 0)
            {
                LoadDataGvwProductType();
            }
        }
        catch (Exception)
        {
            Alert.Show("This product type is being order, you can not delete!");
        }      
    }
    protected void gvwProductType_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvwProductType.PageIndex = e.NewPageIndex;
        LoadDataGvwProductType();
    }
    protected void gvwProductType_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string ProductTypeID = gvwProductType.DataKeys[e.RowIndex].Value.ToString();
        string ProductTypeName = ((TextBox)gvwProductType.Rows[e.RowIndex].Cells[1].Controls[0]).Text;

        if (pProductType.updateProductType(ProductTypeID, ProductTypeName) > 0)
        {
            gvwProductType.EditIndex = -1;
            LoadDataGvwProductType();
        }
    }
}
